//
//  ChooseResonseManController.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/5/5.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "ChooseResonseManController.h"

@interface ChooseResonseManController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView  *_tablview;
    
    NSMutableArray  *_dataArr;
    
    NSMutableArray  *_chooseArr;
}
@end

@implementation ChooseResonseManController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"负责人";
    _dataArr = [NSMutableArray array];
    
    
    
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"back" WithAction:@selector(goToBack)];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(getBackCompetitor:)];
    _tablview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight  -64) style:UITableViewStylePlain];
    _tablview.delegate = self;
    _tablview.dataSource = self;
    [self.view addSubview:_tablview];
    [self getResponseManData];
    //    _tablview.editing = YES;
    _tablview.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (void)getResponseManData
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method" : @"app.village.officials"}];
    
    NSDictionary *parames = [DataTool addSignToDic:dict];
    [DataTool postWithUrl:HEAD_URL parameters:parames success:^(id data) {
      id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        
        if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
            
            NSLog(@"小区负责人的数据 :%@",backData);
            _dataArr = backData[@"data"][@"officials"];
        }else{
//            int code = [[(NSDictionary *)backData objectForKey:@"code"] intValue];
            NSString *msg = [(NSDictionary *)backData objectForKey:@"msg"];
            [Tools showMessage:msg view:self];
        }
        NSLog(@"%@ 负责人获取",backData);
        [_tablview reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        [Tools showMessage:error.localizedDescription view:self];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId= @"chooseResponseId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        
        UIView *line = [ViewTool getLineViewWith:CGRectMake([UIView getWidth:10.0f], 44 - 1, kScreenWidth - 2 * [UIView getWidth:10.0f], 1) withBackgroudColor:GRAYLINECOLOR];
        [cell addSubview:line];
    }
    cell.textLabel.text = _dataArr[indexPath.row][@"realname"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.block(_dataArr[indexPath.row][@"realname"],_dataArr[indexPath.row][@"user_id"]);
    [self goToBack];
}
- (void)goToBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
